package mi.payment.utils.print;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.controller.QRCodeTransactionController;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.SalesSlipPrintSettings;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.utils.BitmapUtils;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.SystemDateTime;
import mi.payment.utils.UIUtils;
import sunmi.payment.L3.L3PrintUtil;
import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;

/**
 * 扫码类小票打印，微信、支付宝
 *
 * @author Created by zdy on 2017/9/8.
 */

public class QRCodeTicketPrinter extends BasePrint {

    private String smallTicketTail; // 自定义内容

    private boolean qrPrintSign = false; // 二维码小票是否打印签名栏（支付宝、微信）

    private PayDetail payDetail;

    public QRCodeTicketPrinter(Context context, IWoyouService printerService) {
        super(context, printerService);
    }

    public String getSmallTicketTail() {
        return smallTicketTail;
    }

    public void setSmallTicketTail(String smallTicketTail) {
        this.smallTicketTail = smallTicketTail;
    }

    public boolean isQrPrintSign() {
        return qrPrintSign;
    }

    public void setQrPrintSign(boolean qrPrintSign) {
        this.qrPrintSign = qrPrintSign;
    }

    public void printQRCodeTicket(PayDetail payDetail, boolean isAgain, final ICallback callback, int pageNum, boolean printQR) throws Exception {
        this.payDetail = payDetail;
        // 是否打印英文
        SalesSlipPrintSettings slipPrintSettings = SalesSlipPrintSettingsOperation.getInstance().getSalesSlipPrintSettings();
        boolean isPrintEnglish = slipPrintSettings.isSalesSlipPrintEnglish();
        int smallSize = 18;
        int normalSize = 22;
        if (slipPrintSettings.getSelectSalesSlipFont() > 0) {
            // 大号字体
            smallSize += 4;
            normalSize += 4;
        }

        printerService.enterPrinterBuffer(true);

        printLogo(callback);

        // 打印存根信息
        boolean isMerchant = printMerchantOrUser(pageNum, isPrintEnglish, smallSize - 2);

        // 打印订单描述
        printOrderDescribes(normalSize);

        printOrderInfo(normalSize);

        // 打印二维码
        printQRCode(callback, printQR, false);

        // 打印商户信息
        printMerchantInfo(normalSize);

        boolean isSign = isQrPrintSign() && isMerchant;
        // 打印持卡人签名
        printCardHolderSign(isPrintEnglish, isSign, null, null, smallSize, smallSize - 4);

        // printReceipt(isPrintEnglish, smallSize, smallSize - 4);

        // 打印服务热线和网址
        // printServerAndWeb(isPrintEnglish, smallSize, smallSize - 4);

        // 重打印
        printAgainText(isAgain, isPrintEnglish, normalSize + 2);

        // 打印小票底部信息
        String smallTicketTail = getSmallTicketTail();
        printFooter(callback, 18, smallTicketTail);

        // L3用户追加打印
        if (pageNum == 1) {
            L3PrintUtil.l3MerchantAddPrinter(printerService);
        } else if (pageNum == 2) {
            L3PrintUtil.l3UserAddPrinter(printerService);
        }

        printerService.lineWrap(4, callback);
        printerService.exitPrinterBufferWithCallback(true, callback);
    }

    private void printLogo(ICallback callback) throws Exception {
        boolean isLogo = Configs.getInstance().IS_PRINT_SCAN_TITLE_IMAGE();
        if (isLogo) {
            printerService.printText("\n", callback);
            printerService.setAlignment(1, callback);
            Bitmap bitmap = new PrintContent().getPrintTitleIcon(payDetail.transPlatform);
            if (bitmap.getWidth() > 384) {
                double size = 1.0 * bitmap.getHeight() * 384 / bitmap.getWidth();
                int newHeight = (int) size;
                bitmap = BitmapUtils.scale(bitmap, 384, newHeight);
            }
            printerService.printBitmap(bitmap, callback);
            printerService.printText("\n", callback);
        }
    }

    /**
     * 打印订单描述
     */
    private void printOrderDescribes(int size) throws Exception {
        String paymentType = UIUtils.getPaymentType(payDetail);
        String title = UIUtils.getQRPaymentTitle(payDetail);
        String amount = MoneyUtils.longCent2DoubleMoneyStr(payDetail.getAmount());
        // 交易类型
        String temp = getString(R.string.payment_type) + paymentType;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 交易金额
        if (payDetail.getTransactionType() == 2 || payDetail.getTransactionType() == 3) {
            setBold(true);
            temp = getString(R.string.revoke_amount) + amount;
            printerService.printTextWithFont(temp + "\n", "", size + 6, null);
            setBold(false);
        } else {
            setBold(true);
            temp = getString(R.string.payment_amount) + amount;
            printerService.printTextWithFont(temp + "\n", "", size + 6, null);
            setBold(false);
            String scanModel;
            if (payDetail.qrCodeScanModel == 1) {
                scanModel = "商户扫码";
            } else {
                scanModel = "客户扫码";
            }
            temp = getString(R.string.order_describes) + title + "(" + scanModel + ")";
            printerService.printTextWithFont(temp + "\n", "", size, null);
        }
        printSeparateLine();
    }

    private void printOrderInfo(int size) throws Exception {
        // 凭证号
        String temp = getString(R.string.receipt_15) + payDetail.voucherNo;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 订单号
        boolean b = Configs.getInstance().isRETURN_GOODS_INDEX_REFERNO();
        if (b) {
            temp = Configs.getInstance().RETURN_GOODS_INDEX_NAME() + ":" + payDetail.referNo;
            printerService.printTextWithFont(temp + "\n", "", size, null);
        } else {
            temp = TextUtils.isEmpty(payDetail.thirdTransNum) ? payDetail.payQRCode : payDetail.thirdTransNum;
            temp = Configs.getInstance().RETURN_GOODS_INDEX_NAME() + ":" + temp;
            printerService.printTextWithFont(temp + "\n", "", size, null);
        }
        // 二维码支付凭证需要添加打印：参考号 批次号
        boolean isPrintMoreInfo = new QRCodeTransactionController().isPrintMoreInfo();
        if (isPrintMoreInfo) {
            // 批次号
            temp = getString(R.string.receipt_10) + payDetail.BathNo;
            printerService.printTextWithFont(temp + "\n", "", size, null);
            // 参考号
            temp = getString(R.string.receipt_11) + payDetail.referNo;
            printerService.printTextWithFont(temp + "\n", "", size, null);
        }
    }

    /**
     * 打印二维码
     */
    private void printQRCode(final ICallback callback, boolean isQRCode, boolean isBarCode) throws Exception {
        if (payDetail.thirdTransNum == null || payDetail.thirdTransNum.length() == 0) return;
        // 打印二维码
        if (isQRCode) {
            printerService.printText("\n", callback);
            printerService.setAlignment(1, null);
            printerService.printQRCode(payDetail.thirdTransNum, 6, 2, callback);
            printerService.setAlignment(0, null);
            printerService.printText("\n", callback);
        }
        // 打印一维码
        if (isBarCode) {
            printerService.printText("\n", callback);
            printerService.setAlignment(1, null);
            printerService.printBarCode(payDetail.thirdTransNum, 8, 100, 2, 2, callback);
            printerService.setAlignment(0, null);
            printerService.printText("\n", callback);
        }
    }

    /**
     * 打印商户信息
     */
    private void printMerchantInfo(int size) throws Exception {
        // 打印商户名称
        String merchantName = TextUtils.isEmpty(payDetail.tempMerchantName) ? payDetail.merchantName : payDetail.tempMerchantName;
        merchantName = TextUtils.isEmpty(merchantName) ? "--" : merchantName;
        String temp = getString(R.string.receipt_3) + merchantName;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 打印商户号
        String merchantNo = TextUtils.isEmpty(payDetail.tempMerchantNo) ? payDetail.merchantNo : payDetail.tempMerchantNo;
        merchantNo = TextUtils.isEmpty(merchantNo) ? "--" : merchantNo;
        temp = getString(R.string.receipt_4) + merchantNo;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 打印终端号
        String terminalNo = TextUtils.isEmpty(payDetail.tempTerminalNo) ? payDetail.terminalNo : payDetail.tempTerminalNo;
        terminalNo = TextUtils.isEmpty(terminalNo) ? "--" : terminalNo;
        temp = getString(R.string.receipt_5) + terminalNo;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 操作员号
        temp = getString(R.string.receipt_20) + payDetail.operatorNo;
        printerService.printTextWithFont(temp + "\n", "", size, null);
        // 日期
        String printerData = getString(R.string.receipt_12) + SystemDateTime.getTimeByPayDetails(payDetail);
        printerService.printTextWithFont(printerData + "\n", "", size, null);
    }


}
